package com.sqllog.admin.auth.process;

import com.alibaba.fastjson.JSON;
import com.sqllog.admin.auth.domain.response.Result;
import com.sqllog.admin.framework.tools.ServletUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义 Security 用户未登陆处理类
 *
 * @author chennengcheng
 * @date 2022-01-14 14:35
 * @remark
 */

@Component
public class SecureAuthenticationSuccessHandler implements AuthenticationSuccessHandler {


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
        request.getSession().setAttribute("currentUser", authentication.getPrincipal());
        Result result = Result.success("登录成功");
        ServletUtil.write(JSON.toJSONString(result));
    }
}
